﻿using System;
using HiTeachers.Entities;
using HiTeachers.Services;

namespace HiTeachers.Web
{
    public partial class Default : PageBase
    {
        private const int NumNewInPage = 12;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            if (!Page.User.Identity.IsAuthenticated)
            {
                mtvDefault.SetActiveView(vAnonymous);
                BindingStdRequest();
                BindingNewestTeacherRepeat();
            }
            else
            {
                mtvDefault.SetActiveView(vAuthen);
                BindingAllRequests();
            }

            BindingMetaData("Home");
            var siteConfig = SiteConfigServices.GetByTextKey("Home");
            if (siteConfig != null) Page.Title = siteConfig.Value;
        }

        #region Private Methods
        private void BindingNewestTeacherRepeat()
        {
            //rptMember.DataSource = TeacherServices.GetNewestTeachers(12);
            //rptMember.DataBind();

            rtrMember.DataSource = TeacherServices.GetNewestTeachers(12);
            rtrMember.DataBind();
        }

        private void BindingStdRequest()
        {
            rptStdRequest.DataSource = StudentRequestServices.GetVerifiedRequest(20, 0);
            rptStdRequest.DataBind();
        }

        private void BindingAllRequests()
        {
            var curPage = string.IsNullOrEmpty(Request["p"]) ? 0 : Convert.ToInt32(Request["p"]) - 1;
            rptAllStrReq.DataSource = StudentRequestServices.GetVerifiedRequest(NumNewInPage, curPage);
            rptAllStrReq.DataBind();

            BindingPage();
        }

        private void BindingPage()
        {
            var totalNew = StudentRequestServices.TotalVerifiedRequest();
            var totalPage = (int)Math.Ceiling((double)totalNew / NumNewInPage);
            var curPage = string.IsNullOrEmpty(Request["p"]) ? 1 : Convert.ToInt32(Request["p"]);

            BindingPaper(ltrPaper, "Default.aspx", curPage, totalPage);
        }
        #endregion
    }
}
